package edu.mtu.citizenscience.cs.sdk.uploader.upload.parts;

import java.util.List;

import edu.mtu.citizenscience.cs.sdk.uploader.CitsciUploaderService;
import edu.mtu.citizenscience.cs.sdk.uploader.model.Row;
import edu.mtu.citizenscience.cs.sdk.uploader.upload.UploadPart;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Uploads reports to the server
 */
public class Reports extends UploadPart {
	// the main upload
	private Upload upload;
	// the report rows
	private List<Row> rows;
	// the current report num
	private int reportNum;
	
	/**
	 * Create a new Reports Upload
	 * 
	 * @param   uploader - CitsciUploaderService - the uploader service uploading the report
	 * @param   upload - Upload - the main upload
	 * @param   uploadId - int - the upload id
	 * @param   rows - List<Row> - the report rows
	 * @return	void
	 */
	public Reports(CitsciUploaderService uploader, Upload upload, List<Row> rows)
	{
		super(uploader);
		
		this.upload = upload;
		this.rows = rows;
	}
	
	/**
	 * Start the reports upload
	 * 
	 * @param   void
	 * @return	void
	 */
	public void startUpload()
	{
		// set the report num to 0
		this.reportNum = 0;
		
		// upload the first report
		this.nextReport();
	}
	
	/**
	 * Upload the next report
	 * 
	 * @param   void
	 * @return	void
	 */
	public void nextReport()
	{
		// check if the current report number is less then the number of reports
		if (this.reportNum < this.rows.size())
		{
			// create new report upload and start it
			new Report(this.uploader, this, this.upload.id, this.rows.get(this.reportNum)).startUpload(this.reportNum + 1);
			// Increment the report number
			this.reportNum++;
		}
		else
		{
			// complete the upload
			this.upload.completeUpload();
		}
	}
	
}
